package GUI;

import order.*;
import model.*;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import GUI.MapPanel.TerritoryPanel;

public class MoveFrame extends OrderFrame{
	 private JComboBox<Integer>[] troopAmountBox;
	 private JComboBox<Integer> destinationBox; 
     private Move temp;

	 public MoveFrame(TextPanel textPanel, TerritoryPanel territoryPanel, Map map){
		super(textPanel, territoryPanel, map);
		
		this.setLayout(new GridLayout(9, 2, 10, 10));	   
		
	  	this.add(new JLabel("Move from " + territory.getLocation()));
	  	this.add(new JLabel("    "));
	  	
	  	troopAmountBox = new JComboBox[6];
	  	
	  	int i = 0;
	  	for (TechnologyLevel level: TechnologyLevel.values()){
	  	   this.add(new JLabel(level.toString()));
	  	   troopAmountBox[i] = new JComboBox<>();
	  	   troopAmountBox[i].addItem(0);
	  	   for (int j = 1; j <= territory.getUsableTroopAmount(level); j++)
	  		   troopAmountBox[i].addItem(j);
	  	   this.add(troopAmountBox[i]);
	  	   i++;
	  	}
	  	
	  	this.add(new JLabel("To Destiantion: "));
	  	destinationBox = new JComboBox<Integer>();
	  	for (Territory t : player.getTerritoryList())
	  	   if (map.routeProcess(territory, t))
	  		destinationBox.addItem(t.getLocation());
	  	
	  	this.add(destinationBox);
	  	
	  	this.add(submitButton);
	  	this.add(cancelButton);
	  	submitButton.addActionListener(new submitListener(this));
	  	cancelButton.addActionListener(new cancelListener(this));
	  	
	  	setSize(600, 600);
	  	setVisible(true);
	 }
	 
	 class submitListener implements ActionListener{
   
		JFrame frame;
	  public submitListener(JFrame _frame){
			frame = _frame;
		}
	  public void actionPerformed(ActionEvent arg0) {
		Territory destination = map.getTerritory((int)destinationBox.getSelectedItem());
		int i = 0;
		HashMap<TechnologyLevel, Integer> trooplist = new HashMap<TechnologyLevel, Integer>();
		for (TechnologyLevel level:TechnologyLevel.values()){
		   trooplist.put(level, (int)troopAmountBox[i].getSelectedItem());
		   i++;
		}  
	  	 temp = new Move(territory, player, destination, trooplist);
	  	 if (temp.checkOrder(map)){
		  	    textPanel.addHistory(temp);
		  	    temp.executeOrder();
		  	    textPanel.updatePlayerPanel();
		  	    territoryPanel.update();
		  	    territoryPanel.getMapPanel().getTerritoryPanel(destination).update();
		  }
	  	 frame.dispose();
	}
	 }
	 
}
